﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ContactUs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            var credentialUserName = WebConfigurationManager.AppSettings["EMAIL_FROM_ADDRESS"];
            var toAddress = WebConfigurationManager.AppSettings["INFO_EMAIL_ADDRESS"];
            var fromAddress = txtEmailAddress.Text.Trim();
            var credentialPassword = WebConfigurationManager.AppSettings["EMAIL_FROM_PASSWORD"];
            var smtpAddress = WebConfigurationManager.AppSettings["SMTP_ADDRESS"];
            var smtPort = WebConfigurationManager.AppSettings["SMTP_PORT"];
            string subject = "Contact Us";

            /////////////Get Body///////////
            string body = txtMessage.Text.Trim();

            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromAddress, toAddress, subject, body);
            //mail.Bcc.Add("manobesh1982@gmail.com");
            mail.IsBodyHtml = true;

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = smtpAddress;
            smtp.Port = Convert.ToInt32(smtPort);
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            //smtp.Credentials = new System.Net.NetworkCredential(credentialUserName, credentialPassword);
            smtp.EnableSsl = false;
            smtp.Send(mail);
            ShowStatusMsg("Mail sent successfully");
            ClearAllFields();
        }
        catch (Exception ex)
        {
            ShowStatusMsg(ex.Message);
        }
    }

    private void ClearAllFields()
    {
        txtFullName.Text = txtEmailAddress.Text = txtMessage.Text = string.Empty;
    }

    private void ShowStatusMsg(string msg)
    {
        lblMsg.Visible = true;
        lblMsg.Text = msg;
    }

    
}